package user;

import book.Book;
import book.Library;
import utils.LibrarySingleton;

import java.time.LocalDate;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-08-21
 * Time: 12:53
 */
public class AdminUser extends User {
    private Scanner sc = new Scanner(System.in);
    private Library library = LibrarySingleton.createLibrary();;

    public AdminUser(String userName, int userId) {
        super(userName, userId, "管理员");
    }

    public int showMenu() {
        System.out.println("管理员" + userName + "的操作菜单");
        System.out.println("1.查找图书");
        System.out.println("2.打印所有的图书");
        System.out.println("3.退出系统");
        System.out.println("4.上架图书");
        System.out.println("5.修改图书");
        System.out.println("6.下架图书");
        System.out.println("7.统计借阅次数");
        System.out.println("8.查看最受欢迎的前n本图书");
        System.out.println("9.查看库存状况");
        System.out.println("10.检查超过一年未下架的图书");
        System.out.println("请选择你的操作");
        return sc.nextInt();
    }
    //上架图书
    public void addBook() {
        //清理缓存区的字符串
        sc.nextLine();
        System.out.println("请输入图书的书名：");
        String bookName = sc.nextLine();
        System.out.println("请输入图书的作者：");
        String author = sc.nextLine();
        System.out.println("请输入图书的类别：");
        String category = sc.nextLine();
        System.out.println("请输入图书的出版年份：");
        int publishYear = sc.nextInt();
        sc.nextLine();
        LocalDate shelfDate = LocalDate.now();
        //创建一本书的对象
        Book book = new Book(bookName,author,category,publishYear,shelfDate);
        //调用图书类里面的添加图书方法
        library.addBook(book);
    }
    //修改图书 可修改书名，作者，类别
    public void updateBook() {
        //显示所有图书
        System.out.println("所有图书信息如下：");
        library.displayBooks();
        System.out.println("请输入你要修改的图书id：");
        int bookId = sc.nextInt();
        //清理回车键
        sc.nextLine();
        Book book = library.searchBook(bookId);
        if(book == null) {
            System.out.println("没有找到对应id的图书");
            return;
        }
        System.out.println("当前书名：" + book.getBookName());
        System.out.println("请输入新的书名：");
        String newBookName = sc.nextLine();

        System.out.println("当前作者：" + book.getAuthor());
        System.out.println("请输入新的作者：");
        String newAuthor = sc.nextLine();

        System.out.println("当前类别：" + book.getCategory());
        System.out.println("请输入新的类别：");
        String newCategory = sc.nextLine();

        book.setBookName(newBookName);
        book.setAuthor(newAuthor);
        book.setCategory(newCategory);

        library.upDateBook(book);
    }
    //下架图书
    public void removeBook() {
        library.displayBooks();
        System.out.println("请输入你要下架的图书id:");
        int bookId = sc.nextInt();
        sc.nextLine();

        Book book = library.searchBook(bookId);

        library.removeBook(bookId);

        System.out.println(book.getBookName() + "已经被删除");
    }
    //统计借阅次数
    public void borrowNumBook() {
        library.borrowNumBook();
    }
    //查看最受欢迎的前n本书
    public void popularBook() {
        System.out.println("请输入最受欢迎的前几本书：" + "(输入数不能超过" + library.getBookCount() + ")");
        int n = sc.nextInt();
        if(n <= 0 && n >library.getBookCount()) {
            System.out.println("输入错误，未找到最受欢迎的前n本书");
            return;
        }
        library.popularBook(n);
    }
    //查看库存状态
    public void numBook() {
        library.numBook();
    }
    //移除上架超过一年的书
    public void removeOneBook() {
        library.removeOneBook();
    }

}
